package lumis.service.mailmarketing;

import org.hibernate.Session;

import lumis.doui.processaction.ProcessActionHandler;
import lumis.portal.PortalException;
import lumis.portal.dao.hibernate.ITransactionHibernate;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.sendmail.IMailBody;
import lumis.portal.sendmail.ISingleMail;

/**
 * Sends a test email to the given user
 *
 * @version $Revision: 6296 $ $Date: 2007-03-29 17:31:15 -0300 (Thu, 29 Mar 2007) $
 * @since 4.0.6
 */
public class MailMarketingSendTestEmailProcessActionHandler extends ProcessActionHandler 
{
	public void processAction() throws PortalException 
	{
		// TODO Auto-generated method stub
		String bulletinId = (String)parameters.get("bulletinId");
		String toEmail = (String)parameters.get("email");

		ITransactionHibernate transactionHib = (ITransactionHibernate) transaction;
		Session session = transactionHib.getSession();
		
		Bulletin bulletin = (Bulletin)session.get(Bulletin.class, bulletinId);
		
		// send email for this bulletin
		ISingleMail singleMail = ManagerFactory.getSendMailManager().createSingleMail();
		
		singleMail.setSourceComponent("STR_MAIL_MARKETING_TEST");
		singleMail.setSourceComponentStringsUrl("lumis/service/mailmarketing/strings/strings");
		singleMail.setFrom(bulletin.getSenderEmail());
		singleMail.setTo(toEmail);
		singleMail.setSubject(localize("STR_TEST")+": "+bulletin.getSubject());
		IMailBody emailBody = singleMail.getBody();
		emailBody.setHtmlMsg(bulletin.getContent());
		
		ManagerFactory.getSendMailManager().addMailToSendQueue(sessionConfig, singleMail, null, transaction);
		
		addDefaultResponse();
	}

}
